package com.filldream.fastboot.common.okutil.stock;

import com.filldream.fastboot.common.stockUtils.qt.entity.StockInfo;
import com.filldream.fastboot.common.stockUtils.qt.StockUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * 股票交易信息
 */
@Data
@Accessors(chain = true)
public class StockOrder extends TransactionBase {

    @ApiModelProperty(value = "股票代码",required = true)
    private String code;

    @ApiModelProperty(value = "购买数量",required = true)
    private String count = "100";

    @ApiModelProperty(value = "购买价格",required = true)
    private String price;

    @ApiModelProperty(value = "预计利润,默认0元保本")
    private String targetProfit = "0";

    @ApiModelProperty(value = "盈余")
    private BigDecimal surplus;

    @ApiModelProperty(value = "股票信息")
    private StockInfo stockInfo;

    public StockOrder() {}
    public StockOrder(String code, String price, String count) { this.code = code; this.count = count; this.price = price; }

    /**
     * 购买成本 =(购买数量*购买价格) + 购买手续费
     * @return
     */
    public BigDecimal getCost(){
        BigDecimal cost = new BigDecimal(price);
        cost = cost.multiply( new BigDecimal(count) );
        return cost.add( getCostPre() );
    }

    /**
     * 购买手续费 = 佣金 + 过户费
     * @return
     */
    public BigDecimal getCostPre(){
        BigDecimal cost = new BigDecimal(price);
        cost = cost.multiply( new BigDecimal(count) );
        return getYj(cost).add( getGhf(cost) );
    }

    /**
     * 获取卖出所得 = (购买数量*新价格) - 佣金 - 印花税 - 过户费
     * @param nowPrice
     * @return
     */
    public BigDecimal getProfitPre(String nowPrice){
        BigDecimal sum = new BigDecimal(nowPrice).multiply( new BigDecimal(this.count) );
        BigDecimal yj = getYj(sum);
        BigDecimal yhs = getYhs(sum);
        BigDecimal changeFee = getGhf(sum);
        return sum.subtract( yj ).subtract( yhs ).subtract( changeFee );
    }

    /**
     * 计算利润 =卖出所得 - 成本
     * 默认保留三位小数
     * @param nowPrice
     * @return
     */
    public BigDecimal getProfit(String nowPrice){
        return getProfitPre(nowPrice).subtract( getCost() ).setScale(3, RoundingMode.DOWN);
    }

    /**
     * 获取商品详情
     * @return
     */
    public StockInfo getStockInfo(){
        List<StockInfo> list = StockUtil.getByCodes(code);
        if(ObjectUtils.isNotEmpty(list)){
            this.stockInfo = list.get(0);
        }
        return this.stockInfo;
    }

    /**
     * 计算需要涨到多少价格才能达到目标利润
     * @param targetProfit     目标利润
     * @return
     */
    public String getNeedPrice(String targetProfit) {
        if( StringUtils.isBlank(targetProfit) ){  targetProfit = "0";  }
        //价格递增
        BigDecimal tempPrice = new BigDecimal(price);
        boolean isOk = getProfit(tempPrice.toString()).compareTo( new BigDecimal(targetProfit) ) >= 0;

        //计算是否满足利润
        while ( !isOk ) {
            tempPrice = tempPrice.add( new BigDecimal("0.01") );
            BigDecimal tempProfit = getProfit(tempPrice.toString());
            isOk = tempProfit.compareTo( new BigDecimal(targetProfit) ) >= 0;
        }
        //tempPrice四舍五入保留两位小数
        return tempPrice.setScale(2, RoundingMode.HALF_UP).toString();
    }

    public String getNeedPrice() {
       return getNeedPrice(targetProfit);
    }

    /**
     * 获取做T后的买入价格
     * 相当于获取做T后的price价格
     * @param stockOrderList
     * @param sellingPrice
     * @return
     */
    public static String doT(List<StockOrder> stockOrderList,String sellingPrice){
        int size = stockOrderList.size();
        String result = "0";
        if(size == 1){
            result = stockOrderList.get(0).getPrice();
        }else  if( size >= 2 ){
            StockOrder stockOrder = stockOrderList.get(0);
            String oldPrice = stockOrder.getPrice();
            String count0 = stockOrder.getCount();
            BigDecimal costPre = stockOrder.getCostPre();
            costPre = costPre.divide(new BigDecimal(count0), 4, RoundingMode.DOWN);
            for (int i = 1; i < stockOrderList.size(); i++) {
                BigDecimal profit = stockOrderList.get(i).getProfit(sellingPrice);
                BigDecimal divide = profit.divide(new BigDecimal(count0), 4, RoundingMode.DOWN);
                result = (new BigDecimal(oldPrice).subtract(divide)).subtract(costPre).toString();
            }
        }else{
            return result;
        }
        return result;
    }

    public static void main(String[] args) {

    }
}
